<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { getHospitalLevelAndRegionService } from '@/apis/home'
import type {
  HospitalLevelAndRegionArr,
  HospitalLevelAndRegionResponseData
} from '@/apis/home/type'

// 等级
const levelList = ref<HospitalLevelAndRegionArr>([])
// 区域
const regionList = ref<HospitalLevelAndRegionArr>([])
// 获取地区和等级数据
const getLevelList = async () => {
  const res: HospitalLevelAndRegionResponseData =
    await getHospitalLevelAndRegionService('Hostype')
  levelList.value = res.data
}
const getRegionList = async () => {
  const res: HospitalLevelAndRegionResponseData =
    await getHospitalLevelAndRegionService('Beijin')
  regionList.value = res.data
}
// active
const activeRegion = ref<string>('')
const activeLevel = ref<string>('')

//监听
const emits = defineEmits(['levelRegionChange'])
watch([activeLevel, activeRegion], ([newLevel, newRegion]) => {
  emits('levelRegionChange', {
    level: newLevel,
    region: newRegion
  })
})
// 渲染完毕
onMounted(() => {
  getLevelList()
  getRegionList()
})
</script>

<template>
  <div class="level">
    <h3>医院</h3>
    <div class="hospital-level">
      <h3>等级:</h3>
      <ul>
        <li :class="{ active: activeLevel === '' }" @click="activeLevel = ''">
          全部
        </li>
        <li
          v-for="level in levelList"
          :key="level.id"
          :class="{ active: activeLevel === level.value }"
          @click="activeLevel = level.value"
        >
          {{ level.name }}
        </li>
      </ul>
    </div>
    <div class="hospital-area">
      <h3>地区:</h3>
      <ul>
        <li :class="{ active: activeRegion === '' }" @click="activeRegion = ''">
          全部
        </li>
        <li
          v-for="region in regionList"
          :key="region.id"
          @click="activeRegion = region.value"
          :class="{ active: activeRegion === region.value }"
        >
          {{ region.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.level {
  color: #7f7f7f;
  margin: 30px 0;
  .hospital-level {
    display: flex;
    margin: 10px 0;
    h3 {
      width: 70px;
    }
    ul {
      display: flex;

      li {
        margin-right: 10px;

        &.active,
        &:hover {
          cursor: pointer;
          color: #55a6fe;
        }
      }
    }
  }
  .hospital-area {
    display: flex;
    h3 {
      width: 70px;
    }
    ul {
      display: flex;
      justify-content: start;
      flex-wrap: wrap;
      li {
        margin-bottom: 10px;
        margin-right: 10px;
        &.active,
        &:hover {
          cursor: pointer;
          color: #55a6fe;
        }
      }
    }
  }
}
</style>
